package com.laihui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.laihui.MyApplication;
import com.laihui.activity.R;
import com.laihui.adapter.TenderListAdapter;
import com.laihui.base.BaseFragment;
import com.laihui.entity.LoanBean;
import com.laihui.entity.LoanItem;
import com.laihui.entity.LoanList;
import com.laihui.entity.TenderList;
import com.laihui.model.InvestDetail;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.Unbinder;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 持有中
 */
public class TenderListFragment extends BaseFragment {
    @BindView(R.id.easy_recycler_view)
    EasyRecyclerView easyRecyclerView;

    //private View header;
    Unbinder unbinder;
    private Activity mActivity;
    private MyApplication application;
    private TenderListAdapter adapter;
    //当前页数
    private int pageNum = 1;
    //每一页显示的行数
    private int epage = 10;
    private String mParam1;
    private static final String ARG_PARAM1 = "param1";
    private static final String TAG = "TenderListFragment";
    LoanBean loan;
    public TenderListFragment() {
    }

    public static TenderListFragment newInstance(String param1) {
        TenderListFragment fragment = new TenderListFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    public void init() {

        mActivity = getActivity();
        application = (MyApplication) mActivity.getApplication();
        easyRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        loan = (LoanBean) getArguments().getSerializable("data");
        adapter = new TenderListAdapter(mActivity);
        easyRecyclerView.setAdapter(adapter);
        getData(loan);

        easyRecyclerView.setErrorView(R.layout.error_layout);


//
//        easyRecyclerView.setRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//
//              //  adapter.clear();
//               // adapter.addAll(investDetail.getTenders());
//            }
//        });
        adapter.setMore(R.layout.load_none, new RecyclerArrayAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
               // pageNum++;
                //adapter.clear();
              //  adapter.addAll(investDetail.getTenders());
            }
        });


        adapter.addHeader(new RecyclerArrayAdapter.ItemView() {
            @Override
            public View onCreateView(ViewGroup parent) {
                return LayoutInflater.from(getContext()).inflate(R.layout.item_tenderlist_header, null);
            }
            @Override
            public void onBindView(View headerView) {
                //这里的处理别忘了
                ((ViewGroup)headerView).requestDisallowInterceptTouchEvent(true);
            }
        });

    }
    public void getData(LoanBean loan){
//        adapter.addAll(investDetail.getTenders());
        HashMap map = new HashMap();
        map.put("loanId", this.loan.id+"");
        Log.e("loanId",this.loan.id+"");
        application.apiService.tenderlist(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<TenderList>() {
                    @Override
                    public void onCompleted() {

                    }
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(TenderList tenderlist) {
                        adapter.clear();
                       adapter.addAll(tenderlist.list);
                    }
                });
    }



    @Override
    public int getFragmentId() {
        return R.layout.fragment_tender_list;
    }

    @Override
    public Object bindFragment() {
        return this;
    }



}
